package com.cuke.entityhelper.actions.gradleRepository;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

public class InsertGradleRepository4Ali extends AnAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {

        if (!isAvailable(e)) {
            return;
        }
        //PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
        //PsiFile file = e.getData(PlatformDataKeys.PSI_FILE);
        //获取光标所在元素
        //PsiElement element = PsiElementUtils.getElement(editor, file);
        Project project = e.getProject();
        Editor editor = e.getData(PlatformDataKeys.EDITOR);
        Document document = editor.getDocument();
        int offset = editor.getCaretModel().getOffset();
        //PsiElement elementAt = psiFile.findElementAt(offset);
        //WriteCommandAction.runWriteCommandAction(project,
        //        () -> document.replaceString(offset, getAliRepositoryText()));
        WriteCommandAction.runWriteCommandAction(project,
                () -> document.insertString(offset, getAliRepositoryText()));
    }

    private static String getAliRepositoryText() {
        String text = "maven {url 'https://maven.aliyun.com/repository/apache-snapshots'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/central'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/google'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/gradle-plugin'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/jcenter'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/spring'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/spring-plugin'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/public'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/releases'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/snapshots'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/grails-core'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/mapr-public'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/staging-alpha'}\n" +
                "    maven {url 'https://maven.aliyun.com/repository/staging-alpha-group'}";
        return text;
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        e.getPresentation().setVisible(isAvailable(e));
    }

    private Boolean isAvailable(AnActionEvent e) {
        Project project = e.getProject();
        Editor editor = e.getData(PlatformDataKeys.EDITOR);
        PsiFile file = e.getData(PlatformDataKeys.PSI_FILE);
        if (project == null || editor == null || file == null) {
            return false;
        }
        if (!"build.gradle".equals(file.getName())) {
            return false;
        }
        return true;
    }
}
